import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)


const routes = [{
    path: '/detail/:id',
    component: () => import('@/views/Detail.vue')
  },
  {
    path: '/login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/register',
    component: () => import('@/views/Register.vue')
  },
  {
    path: '/',
    component: () => import('@/views/Home.vue'),
    redirect: '/article',
    children: [{
        path: 'article',
        component: () => import('@/views/Layout/Article')
      },
      {
        path: 'collect',
        component: () => import('@/views/Layout/Collect')
      },
      {
        path: 'like',
        component: () => import('@/views/Layout/Like')
      },
      {
        path: 'user',
        component: () => import('@/views/Layout/User')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})


// 全局前置导航守卫
router.beforeEach((to, from, next) => {
  // console.log(to) // to.path 表示跳转的路由地址
  // next()
  if (to.path === '/login' || to.path === '/register') {
    next()
  } else {
    if (localStorage.getItem('mobile-token')) {
      next()
    } else {
      next('/login')
    }
  }
})


export default router
